package cn.unuuc.cwl.storage.service;

import cn.unuuc.cwl.common.exception.ServiceException;
import cn.unuuc.cwl.storage.entity.Storage;
import cn.unuuc.cwl.storage.mapper.StorageMapper;
import cn.unuuc.cwl.storage.model.StorageQo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class StorageService extends ServiceImpl<StorageMapper, Storage> {

    public void deductStorage(StorageQo storageQo) {
        Storage storage = this.baseMapper.selectById(storageQo.getStorageId());
        if (storage == null) {
            throw new ServiceException("商品库存不存在");
        }
        if ((storage.getStorageNum() - storageQo.getDeductNum()) < 0) {
            throw new ServiceException("商品库存不足");
        }
        storage.setStorageNum(storage.getStorageNum() - storageQo.getDeductNum());
        this.baseMapper.updateById(storage);
    }
}
